"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketClient = void 0;
const Logger_1 = require("../utils/Logger");
const ProtoUtils_1 = require("../socketserver/protocol/ProtoUtils");
const ProtoConfig_1 = require("../socketserver/protocol/ProtoConfig");
const SocketMsg_1 = require("../socketserver/socket/SocketMsg");
const SocketRequestData_1 = require("./SocketRequestData");
const SocketClientReqManager_1 = require("./SocketClientReqManager");
const ws = require("ws");
class WebSocketClient {
    constructor() {
        this.requestMsgQue = [];
        this.reqId = 1;
        this.respCheckQue = [];
    }
    init(url, id) {
        if (!this.isConnected && !this.isConnecting) {
            this.isConnecting = true;
            this.url = url;
            this.id = id;
            this.ws = new ws(url);
            Logger_1.Logger.log("WebSocketClient===init=", url, this.id);
            // this.ws = new ws('ws://192.168.0.108:8000');
            this.ws.on('open', () => {
                WebSocketClient.count++;
                this.isConnecting = false;
                Logger_1.Logger.info("ws onConnect===", this.url, this.id, WebSocketClient.count);
                this.isConnected = true;
                this.checkSendPack();
            });
            this.ws.on('message', (socketData) => {
                this.onGameMsg(socketData);
            });
            this.ws.on("error", (err) => {
                this.isConnected = false;
                this.isConnecting = false;
                Logger_1.Logger.error("WebSocketClient error=====", this.id, err);
            });
            this.ws.on('close', () => {
                this.isConnected = false;
                this.isConnecting = false;
                Logger_1.Logger.info('disconnected', this.id, this.url);
            });
        }
    }
    onGameMsg(socketData) {
        try {
            let byteArray = new Uint8Array(socketData);
            let totalPack = ProtoUtils_1.ProtoUtils.DeserializeBinary(ProtoConfig_1.ProtoConfig.PB_MESSAGE, byteArray);
            let reqId = totalPack.getSeq();
            let protoType = totalPack.getHead();
            let protoData = totalPack.getData();
            let protoName = ProtoConfig_1.ProtoConfig.getProtoReqName(protoType);
            let msg = SocketMsg_1.SocketMsg.createSocketMsg(reqId, protoType, protoName, protoData);
            let sockeRequestData = this.getRespCheck(reqId);
            Logger_1.Logger.log("onGameMsg===", reqId, protoName, this.respCheckQue, sockeRequestData);
            if (sockeRequestData && sockeRequestData.callback) {
                sockeRequestData.callback(msg, this);
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.onSocketMsg(msg, this);
            }
        }
        catch (error) {
            Logger_1.Logger.error("onGameMsg======error==", error);
        }
    }
    getRespCheck(reqId) {
        for (let i = 0; i < this.respCheckQue.length; i++) {
            let resp = this.respCheckQue[i];
            if (resp.id == reqId) {
                this.respCheckQue.splice(i, 1);
                return resp;
            }
        }
    }
    sendProto(protoPack, callback = null) {
        let reqData = new SocketRequestData_1.default();
        reqData.id = this.reqId;
        this.reqId++;
        reqData.data = protoPack;
        reqData.callback = callback;
        this.requestMsgQue.push(reqData);
        if (this.isConnected) {
            this.checkSendPack();
        }
        else {
            this.init(this.url, this.id);
        }
    }
    checkSendPack() {
        if (this.requestMsgQue.length > 0) {
            if (this.isConnected) {
                let socketData = this.requestMsgQue.shift();
                let serialdata = this.createSendPack(socketData);
                let arrayBuffer = serialdata.buffer;
                this.respCheckQue.push(socketData);
                this.send(arrayBuffer);
                this.checkSendPack();
            }
        }
    }
    /**
    * 创建发送包
    * @param protoPack
    */
    createSendPack(socketData) {
        let protoName = ProtoUtils_1.ProtoUtils.GetProtoName(socketData.data);
        let protoType = ProtoConfig_1.ProtoConfig.getProtoRequestType(protoName);
        let serialdata = socketData.data.serializeBinary();
        let totalPack = ProtoUtils_1.ProtoUtils.Create(ProtoConfig_1.ProtoConfig.PB_MESSAGE);
        totalPack.setSeq(socketData.id);
        totalPack.setHead(protoType);
        totalPack.setData(serialdata);
        Logger_1.Logger.log("createSendPack==", socketData);
        return totalPack.serializeBinary();
    }
    send(data) {
        if (this.isConnected) {
            this.ws.send(data);
        }
    }
}
exports.WebSocketClient = WebSocketClient;
WebSocketClient.count = 0;
